

#Load Packages
library(ggplot2)
library(gridExtra)

# Set this Working Directory to the Folder with the Replication Files
#setwd("C:/Steven/GDrive/Research Files/Clark-Rogers/Descriptive Representation/APSR Final Submission/Replication")

CleanUpSTATA <- function(StataEstimates)
  {
  # Clean up the STATA Output
  StataEstimates$Remove <- substr(StataEstimates$X.,1, 10)
  StataEstimates <- subset(StataEstimates, StataEstimates$Remove=="=_missing_")
  StataEstimates$X. <- gsub("=_missing_",".", StataEstimates$X.)
  
  congress <- data.frame(matrix(NA, nrow(StataEstimates),0))
  congress$pooledestimate <- as.numeric(as.character(StataEstimates$X.))
  return(congress)
  }

CreateStaufferFormat <- function(Margins, Lower, Upper)
  {
  pooledmargin <- CleanUpSTATA(Margins)
  pooledul <- CleanUpSTATA(Upper)
  pooledll <- CleanUpSTATA(Lower)
  congress <- as.data.frame(cbind(pooledmargin,pooledul, pooledll, as.numeric(c(seq(0,60,10)))))
  colnames(congress) <- c("pooledmargin", "pooledll", "pooledul", "pooledr_estimate")
  return(congress)
  }
  
CreatePlot <- function(congress, Label)
  {
  
  #############################################################
  #### Figure 2: Efficacy and Estimates of Cong. Rep.      ####
  #############################################################
  congress.plot<-ggplot(congress, aes(pooledr_estimate, y=pooledmargin, ymin=pooledll, ymax=pooledul))+
    geom_line(size=2) +
    geom_ribbon(size=1.5, alpha=.4, colour=NA)+
    # scale_colour_manual(values=c("forestgreen", "darkorchid4"), name="Gender", labels=c("Female", "Male"))+
    #  scale_fill_manual(values=c("forestgreen", "darkorchid4"), name="Gender", labels=c("Female", "Male"))+
    # scale_colour_manual(values=c("gray35", "gray60"), name="Gender", labels=c("Female", "Male"))+
    #scale_fill_manual(values=c("gray35", "gray60"), name="Gender", labels=c("Female", "Male"))+
    #scale_linetype_manual(values=c("solid", "dotted"), name="Gender", labels=c("Female", "Male"))+
    ylab("Efficacy \n")+
    xlab("Estimate % Women \n ")+
    ggtitle(" ")+
    theme_bw()+
    xlab("")+
    theme(legend.justification=c(1,1), legend.position=c(.25, .25))
  congress.plot<-congress.plot+  xlab(Label)
  congress.plot<-congress.plot+theme_bw() + theme(panel.grid.major = element_blank(),
                                                  panel.grid.minor = element_blank())
  congress.plot<-congress.plot+  theme(axis.text=element_text(size=20),
                                       axis.title=element_text(size=20, face="bold"),
                                       plot.title=element_text(size=22, face="bold"))
  congress.plot<-congress.plot+coord_cartesian(ylim=c(.0, .5))
  congress.plot<-congress.plot+scale_y_continuous(breaks=c(0, .1,.2, .3,.4, .5))
  congress.plot<-congress.plot+scale_x_continuous(breaks=c(0, 10, 20, 30, 40, 50), limits=c(0,50))+
    theme(legend.justification=c(.5,.85), legend.position=c(.20,.95), legend.text=element_text(size=16), 
          legend.title=element_text(size=18))
  #congress<-congress + theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(), panel.background = element_blank(), panel.border=element_blank(), axis.line = element_line(colour = "black"))
  congress.plot<-congress.plot+theme(legend.key.width=unit(1.5,"cm"))
  congress.plot<-congress.plot+guides(colour=guide_legend(override.aes=list(size=1)))+
    theme(panel.spacing = unit(1.5, "lines")) +
    theme(strip.text.x = element_text(size = 16))
  
  congress.plot
  }
  
###
# Stauffer Gender
###

CurrentEstimates <- read.csv("Scripts/Figures/Estimates/Stauffer-FigureA4-Margins.csv")
CurrentUpper <- read.csv("Scripts/Figures/Estimates/Stauffer-FigureA4-upperbound.csv")
CurrentLower <- read.csv("Scripts/Figures/Estimates/Stauffer-FigureA4-lowerbound.csv")

congress <- CreateStaufferFormat(CurrentEstimates, CurrentUpper, CurrentLower)

  tiff(filename = "Figures/Appendix/Figure-A-4-Panel-A.tiff",
      width = 4.5, height = 5, units = "in", pointsize = 12, res=1200)
      CreatePlot(congress, "\n Estimate % Women")
      dev.off()

###
# Rogers Gender
###

      CurrentEstimates <- read.csv("Scripts/Figures/Estimates/Rogers-FigureA4-Gender-Margins.csv")
      CurrentUpper <- read.csv("Scripts/Figures/Estimates/Rogers-FigureA4-Gender-lowerbound.csv")
      CurrentLower <- read.csv("Scripts/Figures/Estimates/Rogers-FigureA4-Gender-upperbound.csv")
      
      congress <- CreateStaufferFormat(CurrentEstimates, CurrentUpper, CurrentLower)
      
      tiff(filename = "Figures/Appendix/Figure-A-4-Panel-B.tiff",
           width = 4.5, height = 5, units = "in", pointsize = 12, res=1200)
      CreatePlot(congress, "\n Estimate % Women")
      dev.off()
      
###
# Rogers Race
###
      
      CurrentEstimates <- read.csv("Scripts/Figures/Estimates/Rogers-FigureA4-Race-Margins.csv")
      CurrentUpper <- read.csv("Scripts/Figures/Estimates/Rogers-FigureA4-Race-lowerbound.csv")
      CurrentLower <- read.csv("Scripts/Figures/Estimates/Rogers-FigureA4-Race-upperbound.csv")
      
      congress <- CreateStaufferFormat(CurrentEstimates, CurrentUpper, CurrentLower)
      
      tiff(filename = "Figures/Appendix/Figure-A-4-Panel-C.tiff",
           width = 4.5, height = 5, units = "in", pointsize = 12, res=1200)
      CreatePlot(congress, "\n Estimate % Black")
      dev.off()